﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AutoFacUtils;
using Common;
using CashFlow.Db.Entity;
using CashFlow.Admin.Validator;
using CashFlow.Admin.Service;
using CashFlow.Admin.Common;
using CashFlow.Admin.Def;
using CashFlow.Admin.Models;
using CashFlow.Admin.Models.Result;

namespace CashFlow.Admin.Controllers.Shop
{
    public class UserController : Controller
    {
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }

        [HttpGet]
        public ActionResult Index()
        {
            var service = GetAdminService();
            var ui = service.GetLoginUserInfo();
            ViewBag.ShopManagerUserId = ui.ShopManagerUserId;
            return View();
        }

        [HttpPost]
        public ActionResult List(UserInfoQueryParamModel model)
        {
            var adminService = GetAdminService();
            var userService = GetUserService();
            var ui = adminService.GetLoginUserInfo();
            var ls = userService.QueryAllChildUsersWithSameShopByUserIdByWhere(
                        ui.ShopManagerUserId
                        , model.Phone
                        , model.Name
                        , model.InvitationCode
                        , model.Reference
                        , model.ReferenceInvitationCode);
            var ls2 = ls.Select(x => Convert(x)).ToList();
            return MyCommonTool.CreateLayUITableResult(true, "", ls2);
        }

        [HttpPost]
        public ActionResult UpdateUser(UpdateUserModel model)
        {
            var userService = GetUserService();
            var result = userService.UpdateUser(model.UserId
                , model.Phone
                , model.Name);
            return MyCommonTool.CreateOperationResult(
                        result == EUpdateUserResult.Success ? true : false
                        , EnumTool.GetDescription<EUpdateUserResult, int>(result)
                        , null
                   );
        }

        public ActionResult ResetPwd(ResetUserPwdModel model)
        {
            var userService = GetUserService();
            var result = userService.ResetPwd(model.UserId
                , model.Pwd
                , model.NewPwd);
            return MyCommonTool.CreateOperationResult(
                        result == EResetUserPwdResult.Success ? true : false
                        , EnumTool.GetDescription<EResetUserPwdResult, int>(result)
                        , null
                   );
        }

        [HttpPost]
        public ActionResult QueryUserStructure(int userId)
        {
            var userService = GetUserService();
            var ls = userService.QueryAllChildUsersWithSameShopByUserId(userId);
            var ls2 = ls.Select(x => x.UserInfo).ToList();
            return MyCommonTool.CreateOperationResult(
                        true
                        , ""
                        , ls2
                   );
        }

        private UserInfoModel Convert(UserInfoAndReferenceUserInfo ur)
        {
            return new UserInfoModel
            {
                Id = ur.UserInfo.Id,
                CreateTime = ur.UserInfo.CreateTime,
                InvitationCode = ur.UserInfo.InvitationCode,
                LoginTime = ur.UserInfo.LoginTime,
                Name = ur.UserInfo.Name,
                Phone = ur.UserInfo.Phone,
                ReferenceInvitationCode = ur.UserInfo.ReferenceInvitationCode,
                ReferenceName = ur.ReferenceUserInfo.Name
            };
        }
    }
}